package com.myk.game.gobangchess.ai.shock;

public class Score_prediction_deep1 {

    public static int[][] score_prediction_deep1(int[][] board, int playerTurn) {
        int[][] minScore = createMatrix(8, 8, -90000);;
        for (int x = 0; x < 8; x++) {
            for (int y = 0; y < 8; y++) {
                if (board[y][x] == 0) {
                    int ifCheck = Checkifcheck.checkifcheck(board, playerTurn, x, y);
                    if (ifCheck == 1) {
                        int[][] newBoard = Pointsetting.pointsetting(board, x, y);
                        newBoard[y][x] = playerTurn;

                        int[] isKillDeep2Result = Kill_judging_deep2.kill_judging_deep2(newBoard, -playerTurn);
                        int isKillDeep2 = isKillDeep2Result[0];

                        if (isKillDeep2 == 0) {
                            int[][] newScoreInner = createMatrix(8, 8, 70000);
                            for (int x1 = 0; x1 < 8; x1++) {
                                for (int y1 = 0; y1 < 8; y1++) {
                                    if (newBoard[y1][x1] == 0) {
                                        ifCheck = Checkifcheck.checkifcheck(newBoard, -playerTurn, x1, y1);
                                        if (ifCheck == 1) {
                                            int[][] newBoard1 = Pointsetting.pointsetting(newBoard, x1, y1);
                                            newBoard1[y1][x1] = -playerTurn;

                                            int[] isKillResult = Kill_judging_deep1.kill_judging_deep1(newBoard1);
                                            int isKill = isKillResult[(int) (0.5 * (1 - playerTurn))];
                                            if (isKill == 0) {
                                                newScoreInner[x1][y1] = playerTurn * Score_weighting.score_weighting(newBoard1, playerTurn);
                                            } else {
                                                newScoreInner[x1][y1] = 70000;
                                            }
                                        }
                                    }
                                }
                            }
                            minScore[x][y] = findMin(newScoreInner);
                        }
                    }
                }
            }
        }
        int[] maxAndIndex = findMax(minScore);
        int bestScore = maxAndIndex[0];
        int linearIndex = maxAndIndex[1];
        int[] point = ind2sub(linearIndex);
        return new int[][]{point, {bestScore}};
    }

    private static int findMin(int[][] array) {
        int min = array[0][0];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                if (array[i][j] < min) {
                    min = array[i][j];
                }
            }
        }
        return min;
    }

    private static int[] findMax(int[][] array) {
        int max = array[0][0];
        int index = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                if (array[i][j] > max) {
                    max = array[i][j];
                    index = i * array[0].length + j + 1;
                }
            }
        }
        return new int[]{max, index};
    }

    private static int[] ind2sub(int index) {
        if(index == 0){
            return new int[]{1,1};
        }
        else{
            int row = (index - 1) / 8;
            int col = (index - 1) % 8;
            return new int[]{row + 1, col + 1};
        }
    }

    private static int[][] createMatrix(int rows, int cols, int value) {
        int[][] matrix = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i][j] = value;
            }
        }
        return matrix;
    }
}
